(function ($) {
		$.fn.setwaterfall = function (options) {
			function findLowestIndex(Arr)
							{
								var index = 0;
								var i;
								for (i in Arr)
								{
									if(Arr[i]<Arr[index])
									{
										index = i;
									}
								}
								return index;
							};

			function findBigestIndex(Arr)
							{
								var index = 0;
								var i;
								for (i in Arr)
								{
									if(Arr[i]>Arr[index])
									{
										index = i;
									}
								}
								return index;
							};

		   var boxwidth = $(this).width()+parseInt($(this).css("paddingLeft"))*2+parseInt($(this).css("marginLeft"))*2;
		   var wrapWidth = $(this).parent().width();
		   var col = Math.floor(wrapWidth/boxwidth);
           var wrappadding = (wrapWidth % boxwidth) /2;
		   var row = Math.floor($(this).length/col) == $(this).length/col?$(this).length/col:(Math.floor($(this).length/col)+1);
		   var colhigharry = [];
		   var colpos;
		   var leftpos;
		   var toppos;
		   for(var len = 0;len < col;len++)
		   {
				colhigharry.push(0);
		   };
		   $(this).each(function(index){
					var pos = index;
					var col1height = 0;
					var col2height = 0;
					var col3height = 0;
					var col4height = 0;
					if(pos<col)
					{
						leftpos = boxwidth*pos + wrappadding + "px";
						$(this).css({"top":"0","left":leftpos});
						colhigharry[index] = $(this).height()+parseInt($(this).css("marginTop"))*2+parseInt($(this).css("paddingTop"))+parseInt($(this).css("paddingBottom"));
					}
					else
					{
					   colpos = findLowestIndex(colhigharry);
					   leftpos = boxwidth*colpos + wrappadding +"px";
					   toppos = colhigharry[colpos]+"px";
					   $(this).css({"top":toppos,"left":leftpos});
					   colhigharry[colpos] =  colhigharry[colpos] + $(this).height()+parseInt($(this).css("marginTop"))*2+parseInt($(this).css("paddingTop"))+parseInt($(this).css("paddingBottom"));
					}           
		   });
		   var wraphighindex = findBigestIndex(colhigharry);
		   var wraphigh = colhigharry[wraphighindex]+"px";
		   $(this).parent().height(wraphigh);           
		   var selector = $(this).selector;
		   window.onresize = function(){$(selector).setwaterfall();};
			}
	})(jQuery)
	
	var loadPage = "false";
	var currpage = 0;
	function getObj(){
	    var obj = [];
	    var url = "/album/demo/loadPage.htm";
	    
	    if(loadPage == "false"){
	        loadPage = "true"
	        currpage = currpage +1;
  			$.post(
				url,
				{"currpage":currpage},
				function(data){
					var list = eval(data);
					for(var i=0;i<list.length;i++){
						var str = '<p>'+list[i].title+'</p>';
						str += '<p align="center"><a href="/album/demo/view.htm?id='+list[i].id+'" target="_blank"><img width="132" height="182" alt="'+list[i].title+'" src="'+list[i].pirurl+'"></a></p>';
						str += '<p>主演：'+list[i].starring+'</p>';
						str += '<p>导演：'+list[i].director+'</p>';
						str += '<p>类型：'+list[i].type+'</p>';
						str += '<p>上映时间：'+list[i].showTime+'</p><hr>';
						str += '<p>'+list[i].introduction+'</p>';
						obj[i+15]=["<li class=\"b hide\">"+str+"</li>"];
					}
					var html = obj.join("");
					$("ul").append(html);
					$(".hide").animate({opacity:1},1000);
					$("ul li").setwaterfall();
					loadPage = "false";
				},"json"
	   		 ); 
	    }
	    return obj;
	}
	
	$(document).ready(function(){
		$("ul li").setwaterfall();
		$(window).scroll(function () {
			var clienth = document.documentElement.clientHeight;
			var scrollh = document.documentElement.scrollHeight;
			var scrollt = document.documentElement.scrollTop + document.body.scrollTop;
			if (clienth + scrollt + 200 > scrollh && currpage >0) {
			    var obj = getObj();
			}
			
			if(currpage == 0) currpage = 1;
		});

	});